package com.whfc.ms.api.mach.controller.v1;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.MachDictDataDTO;
import com.whfc.mach.param.MachDictDataAddParam;
import com.whfc.mach.param.MachDictDataEditParam;
import com.whfc.ms.api.mach.service.MsMachDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 设备数据字典
 *
 * @author sun_guodong
 * @date 2021-08-09
 */
@RestController
@RequestMapping("/ms/api/v1/mach/dict/data")
public class MsMachDictDataController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsMachDictDataService msMachDictDataService;

    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("dictCode") String dictCode,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("字典数据列表(分页),loginer:{},deptId:{},dictCode:{},pageNum:{},pageSize:{}", user.getUsername(), deptId, dictCode, pageNum, pageSize);
        PageData<MachDictDataDTO> pageData = msMachDictDataService.list(deptId, dictCode, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = {"/anon/list", "/list"})
    public Result list(@RequestParam("deptId") Integer deptId,
                       @RequestParam("dictCode") String dictCode) {
        logger.info("字典数据列表(不分页),deptId:{},dictCode:{}", deptId, dictCode);
        ListData<MachDictDataDTO> listData = msMachDictDataService.list(deptId, dictCode);
        return ResultUtil.success(listData);
    }

    @RequiresPermissions("mach/dict/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody MachDictDataAddParam param) {
        logger.info("添加字典数据,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachDictDataService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/dict/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @Validated @RequestBody MachDictDataEditParam param) {
        logger.info("编辑字典数据,loginer:{},param:{}", user.getUsername(), param.toString());
        msMachDictDataService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("mach/dict/del")
    @PostMapping("/del/{id}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("id") Integer id) {
        logger.info("删除字典数据,loginer:{},id:{}", user.getUsername(), id);
        msMachDictDataService.del(id);
        return ResultUtil.success();
    }

}
